package com.fengchao.api.service;

import com.fengchao.api.dao.bci.BciArtistInfoMapper;
import com.fengchao.api.dao.bci.BciArtistSummaryMapper;
import com.fengchao.api.dao.cfi.CfiConcertCrowdfundingSingalMapper;
import com.fengchao.api.dao.mri.MriAlbumMapper;
import com.fengchao.api.dao.mri.MriSongsArtistJoinMapper;
import com.fengchao.api.dao.mri.MriSongsMapper;
import com.fengchao.api.dao.trc.TrcSearchKeyWordMapper;
import com.fengchao.model.bci.BciArtistInfo;
import com.fengchao.model.bci.BciArtistSummary;
import com.fengchao.model.entity.Crowdfunding;
import com.fengchao.model.mri.MriSongs;
import com.fengchao.model.trc.TrcSearchKeyWord;
import com.vdurmont.emoji.Emoji;
import com.vdurmont.emoji.EmojiManager;
import com.vdurmont.emoji.EmojiParser;
import com.vdurmont.emoji.EmojiTrie;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import static java.awt.SystemColor.text;

/**
 * Created by wangnian on 2016/6/30.
 */
@Service
public class SearchService {

    @Autowired
    private BciArtistInfoMapper bciArtistInfoMapper;
    @Autowired
    private CfiConcertCrowdfundingSingalMapper cfiConcertCrowdfundingSingalMapper;
    @Autowired
    private MriSongsMapper mriSongsMapper;
    @Autowired
    private MriAlbumMapper mriAlbumMapper;
    @Autowired
    private MriSongsArtistJoinMapper mriSongsArtistJoinMapper;
    @Autowired
    private TrcSearchKeyWordMapper trcSearchKeyWordMapper;
    @Autowired
    private BciService bciService;
    @Autowired
    private BciArtistSummaryMapper bciArtistSummaryMapper;


    /**
     * 根据name模糊查询艺人Artist
     *
     * @param name
     * @param page
     * @param count
     * @return
     */
    public List<Map<String, Object>> findArtistMapByLikeName(String name, Integer page, Integer count) {
        Integer start = (page - 1) * count;
        List<Map<String, Object>> resultList = new ArrayList<>();
        List<BciArtistInfo> bciArtistInfos = bciArtistInfoMapper.findArtistByLikeName(name, start, count);
        for (BciArtistInfo bciArtistInfo : bciArtistInfos) {
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("id", bciArtistInfo.getId());
            resultMap.put("nameDisplay", bciArtistInfo.getNameDisplay());
            resultMap.put("appImage", bciArtistInfo.getDetailTitleImageUrl());
            resultMap.put("nationality", bciArtistInfo.getNationality());
            resultList.add(resultMap);
        }
        return resultList;
    }


    /**
     * 根据名字模糊查询众筹
     *
     * @param name
     * @param page
     * @param count
     * @return
     */
    public List<Map<String, Object>> findCrowdfundingMapByLikeName(String name, Integer page, Integer count) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        Integer start = (page - 1) * count;
        List<Crowdfunding> list = cfiConcertCrowdfundingSingalMapper.findCrowdfundingByLinkName(name, start, count);
        for (Crowdfunding crowdfunding : list) {
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("image", crowdfunding.getAppSquareImageUrl());
            resultMap.put("title", crowdfunding.getCrowdfundingTitle());
            resultMap.put("time", crowdfunding.getCrowdfundingStartTime());
            resultMap.put("city", crowdfunding.getCity());
            resultMap.put("id", crowdfunding.getId());
            //相关的艺人
            List<BciArtistInfo> bciArtistInfos = bciArtistInfoMapper.findArtistByCrowdfundingId(crowdfunding.getId());
            if (bciArtistInfos.size() > 0) {
                resultMap.put("artistName", bciArtistInfos.get(0).getNameDisplay());
            } else {
                resultMap.put("artistName", "");
            }
            resultList.add(resultMap);
        }
        return resultList;
    }


    /**
     * 根据名字查询歌曲
     *
     * @param name
     * @param page
     * @param count
     * @return
     */
    public List<Map<String, Object>> findMriSongsByLinkName(String name, Integer page, Integer count) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        Integer start = (page - 1) * count;
        List<MriSongs> list = mriSongsMapper.findMriSongsByLinkName(name, start, count);
        for (MriSongs mriSongs : list) {
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("songsId", mriSongs.getId());
            resultMap.put("songsName", mriSongs.getSongName());
            List<Map<String, String>> mriAlbumList = mriAlbumMapper.findMriAlbumBySongId(mriSongs.getId());
            List<Integer> artistIds = mriSongsArtistJoinMapper.findArtistIdBySongId(mriSongs.getId());
            resultMap.put("artistId", artistIds.get(0));
            if (mriAlbumList.size() > 0) {
                resultMap.put("albumId", mriAlbumList.get(0).get("albumId"));
                resultMap.put("albumName", mriAlbumList.get(0).get("albumName"));
            }
            resultList.add(resultMap);
        }
        return resultList;
    }

    /**
     * 根据name模糊查询专辑
     *
     * @param name
     * @param page
     * @param count
     * @return
     */
    public List<Map<String, Object>> findAlbumByLinkName(String name, Integer page, Integer count) {
        // List<Map<String, Object>> resultList = new ArrayList<>();
        Integer start = (page - 1) * count;
        List<Map<String, Object>> mriAlbumList = mriAlbumMapper.findMriAlbumByLinkName(name, start, count);
        return mriAlbumList;
    }


    /**
     * 查询搜索的关键字
     *
     * @param page
     * @param count
     * @return
     */
    public List<Map<String, Object>> findTrcSearchKeyWordList(Integer page, Integer count) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        Integer start = (page - 1) * count;
        List<TrcSearchKeyWord> trcSearchKeyWordList = trcSearchKeyWordMapper.findTrcSearchKeyWordList(start, count);
        for (TrcSearchKeyWord trcSearchKeyWord : trcSearchKeyWordList) {
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("keyWord", trcSearchKeyWord.getKeyWord());
            resultList.add(resultMap);
        }
        return resultList;
    }


    /**
     * 设置关键字
     *
     * @param keyWord
     * @return
     */
    @Transactional
    public boolean setKeyWord(String keyWord) {
        char[] text = keyWord.toCharArray();
        int startPos = 0;
        for (int j = startPos + 1; j <= text.length; ++j) {
            EmojiTrie.Matches status = EmojiManager.isEmoji(Arrays.copyOfRange(text, startPos, j));
            if (status.exactMatch()) {
                return true;
            } else if (status.impossibleMatch()) {
            }
        }
        TrcSearchKeyWord trcSearchKeyWord = trcSearchKeyWordMapper.findTrcSearchKeyWordBykeyWord(keyWord);
        if (trcSearchKeyWord != null) {
            trcSearchKeyWord.setFrequency(trcSearchKeyWord.getFrequency() + 1);
            trcSearchKeyWordMapper.updateByPrimaryKeySelective(trcSearchKeyWord);
        } else {
            trcSearchKeyWord = new TrcSearchKeyWord();
            trcSearchKeyWord.setFrequency(1);
            trcSearchKeyWord.setKeyWord(keyWord);
            trcSearchKeyWord.setRecommend(false);
            trcSearchKeyWord.setUsable(true);
            trcSearchKeyWordMapper.insertSelective(trcSearchKeyWord);
        }
        return true;
    }


    /**
     * 搜索艺人列表(我们的艺人)
     *
     * @param userId
     * @param keyWords
     * @param page
     * @param count
     * @return
     */
    public List<Map<String, Object>> findArtistByNamePyAndPage(Integer userId, String keyWords, Integer page, Integer count) {
        Integer start = (page - 1) * count;
        List<BciArtistInfo> bciArtistInfos = bciArtistInfoMapper.findArtistByLikeName(keyWords, start, count);
        return bciService.forBciArtistInfo(bciArtistInfos, userId);
    }


    /**
     * 搜索心动艺人的列表
     *
     * @param keyWork
     * @param page
     * @param count
     * @return
     */
    public List<Map<String, Object>> getBciArtistSummaryByName(String keyWork, Integer page, Integer count) {
        Integer start = (page - 1) * count;
        List<BciArtistSummary> bciArtistSummaryList = bciArtistSummaryMapper.findBciArtistSummaryOrderByLinkName(keyWork, start, count);
        return bciService.forBciArtistSummaryList(bciArtistSummaryList);
    }

}
